package gdconf

import (
	"server_cluster/common/logger"
)

// 角色等级配置表

type RoleLevelConfig struct {
	Id         int32      `csv:"Id" gorm:"primary_key;autoIncrement:false"` // 角色等级id
	LevelGroup int32      `csv:"LevelGroup,omitempty"`                      // 升级组
	Level      int32      `csv:"Level,omitempty"`                           // 等级
	Exp        int32      `csv:"Exp,omitempty"`                             // 升到下一等级所需经验
	FightProp1 *FightProp `csv:"Attribute1,omitempty"`                      // 攻击值
	FightProp2 *FightProp `csv:"Attribute2,omitempty"`                      // 生命值
	FightProp3 *FightProp `csv:"Attribute3,omitempty"`                      // 防御值

	FightPropList []*FightProp `gorm:"-" csv:"-"` // 战斗属性列表
}

func (r *RoleLevelConfig) TableName() string {
	return "config_role_level"
}

func (g *GameDataConfig) saveRoleLevelConfig() {
	saveTableToDb[RoleLevelConfig](g.Db, readTable[RoleLevelConfig](g.CsvPathPrefix+"role_level.csv"))
}

func (g *GameDataConfig) loadRoleLevelConfig() {
	g.GameDataMaps.RoleLevelConfigMap = make(map[int32]map[int32]*RoleLevelConfig)
	for _, roleLevelConfig := range loadTableFromDb[RoleLevelConfig](g.Db) {
		_, exist := g.GameDataMaps.RoleLevelConfigMap[roleLevelConfig.LevelGroup]
		if !exist {
			g.GameDataMaps.RoleLevelConfigMap[roleLevelConfig.LevelGroup] = make(map[int32]*RoleLevelConfig)
		}
		roleLevelConfig.FightPropList = make([]*FightProp, 0)
		if roleLevelConfig.FightProp1 != nil {
			roleLevelConfig.FightPropList = append(roleLevelConfig.FightPropList, roleLevelConfig.FightProp1)
		}
		if roleLevelConfig.FightProp2 != nil {
			roleLevelConfig.FightPropList = append(roleLevelConfig.FightPropList, roleLevelConfig.FightProp2)
		}
		if roleLevelConfig.FightProp3 != nil {
			roleLevelConfig.FightPropList = append(roleLevelConfig.FightPropList, roleLevelConfig.FightProp3)
		}
		g.GameDataMaps.RoleLevelConfigMap[roleLevelConfig.LevelGroup][roleLevelConfig.Level] = roleLevelConfig
	}
	logger.Info("RoleLevelConfig Count: %v", len(g.GameDataMaps.RoleLevelConfigMap))
}

func GetRoleLevelConfigByGroupAndLevel(group int32, level int32) *RoleLevelConfig {
	value1, exist := CONF.GameDataMaps.RoleLevelConfigMap[group]
	if !exist {
		logger.Error("[ConfigNotFound] RoleLevelConfig, group: %v, level: %v", group, level)
		return nil
	}
	value2, exist := value1[level]
	if !exist {
		logger.Error("[ConfigNotFound] RoleLevelConfig, group: %v, level: %v", group, level)
		return nil
	}
	return value2
}

func GetRoleLevelConfigMap() map[int32]map[int32]*RoleLevelConfig {
	return CONF.GameDataMaps.RoleLevelConfigMap
}

func CheckRoleLevelConfigByGroupAndLevel(group int32, level int32) bool {
	value, exist := CONF.GameDataMaps.RoleLevelConfigMap[group]
	if !exist {
		return false
	}
	_, exist = value[level]
	if !exist {
		return false
	}
	return true
}
